<?php
/**
 * Description of DepartmentMapper
 *
 /*
 * @author     Thibaut Cromphaut and Dimitry Dierickx
 * @copyright  Copyright (c) 2011 Safti-Kado
 */
class Application_Model_DepartmentMapper extends Ahs_Model_Mapper
{

    public function __construct(array $options = null)
    {
        parent::__construct($options);
        $this->DbTableName = 'Application_Model_DbTable_Departments';
    }

    /**
     * Gebruiker met een bepaalde id ophalen
     * 
     * @param integer $id
     * @return Application_Model_Department
     */
    public function fetchById($id)
    {
        $row = $this->getDbTable()->fetchRow(array('department_id = ?' => $id));

        return $row; //$this->_toDepartment($row);
    }
    
    public function fetchByDepartmentId($id)
    {
        $row = $this->getDbTable()->fetchRow(array('department_id = ?' => $id));

        return $this->_toDepartment($row);
    }
    
    public function fetchByName($name)
    {
        $row = $this->getDbTable()->fetchRow(array('department_name = ?' => $name));

        return $row; //$this->_toDepartment($row);
    }

    /**
     * Alle departments ophalen
     * 
     * @return array
     */
    public function fetchAll()
    {        
        $rows = $this->getDbTable()->fetchAll();        
        return $this->_toDepartments($rows);
        //return $this->getDbTable();
    }
    
     /* 
     * @return array
     */    
    public function fetchAllDepartmentsOrder()
    {        
        $sort = $this->getDbTable()->select()->order(array('department_name ASC'));
        $rows = $this->getDbTable()->fetchAll($sort);

        return $this->_toDepartments($rows);
    }

    /**
     * User opslaan
     *
     * @param Application_Model_Department $department
     */
    public function save(Application_Model_Department $department)
    {
        
        //DEPARTMENT ELEMENTS ARE EMPTY
        $data = array(
            'department_name'          => $department->Name,
            //'department_description'   => $department->Description,
            'department_color'         => $department->Color,
            
            
        );

        $departmentHasSubdepartment = new Application_Model_DepartmentHasSubdepartment();
        
        if (null === $department->Id) {
            $this->getDbTable()->insert($data);
        } else {
            $this->getDbTable()->update($data, array('department_id = ?' => $department->Id));                        
            /**
             * @todo OPSLAAN VAN SUBDEPARTMENT (save functie aanmaken)
             */
            //$departmentHasSubdepartment->Courseid = $book->CourseId;           
            
            //$departmentHasSubdepartmentMapper = new Application_Model_DepartmentHasSubdepartment();
            //$departmentHasSubdepartmentMapper->save($departmentHasSubdepartment);
        }
    }
    
    /**
     * @param Zend_Db_Table_Row_Abstract $row
     * @return Application_Model_Department 
     */
    private function _toDepartment(Zend_Db_Table_Row_Abstract $row = null)
    {
       $department = new Application_Model_Department();
       $departmentHasSubdepartmentMapper = new Application_Model_DepartmentHasSubdepartmentMapper();
       //$departmentArray = $departmentHasSubdepartmentMapper->fetchAll();
       
       if ($row) {
                $department->Id             = $row['department_id'];
                $department->Name           = $row['department_name'];
                //$department->Description    = $row['department_description'];    
                $department->Color          = $row['department_color'];
                //Zend_Debug::dump($row['department_id']);
                //Zend_Debug::dump($departmentHasSubdepartmentMapper->fetchAllWithDepartmentId($row['department_id']));                
                $department->Subdepartment = $departmentHasSubdepartmentMapper->fetchAllWithDepartmentId($row['department_id']);                
                
       }

        return $department;
    }

    /**
     * @param Zend_Db_Table_Rowset_Abstract $rows
     * @return array
     */
    private function _toDepartments(Zend_Db_Table_Rowset_Abstract $rows = null)
    {
        $departments = array();

        if ($rows) {
            foreach ($rows as $row) {
                $departments[] = $this->_toDepartment($row);
            }
        }
        
        return $departments;
    }

}